<template>
  <div class="list-page">
    <div class="block-wrap filter-form">
      <el-form :inline="true" :model="form" size="small">
        <el-form-item label="操作人">
          <el-input v-model="form.createBy" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="日志内容">
          <el-input v-model="form.title" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="起止时间" class="dateTime">
          <el-date-picker
            v-model="createTime"
            type="daterange"
            align="right"
            unlink-panels
            format="yyyy-MM-dd"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
            :picker-options="pickerOptions"
          />
        </el-form-item>

        <el-form-item class="filter-form-btns">
          <el-button type="primary" @click="search">查询</el-button>
          <el-button type="default" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="block-wrap">
      <el-table
        :data="dataList"
        style="width: 100%;"
        v-loading="loading"
        align="center"
        header-align="center"
        :header-cell-style="headerStyle"
        :cell-style="cellStyle"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60"
          :index="getIndex"
        />
        <el-table-column prop="updateTime" label="操作时间">
          <template slot-scope="scope">
            {{ getCreateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="操作用户" />
        <el-table-column prop="operation" label="操作类型" />
        <el-table-column prop="requestIp" label="操作IP" />
        <el-table-column prop="title" label="日志内容" />
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="form.limit"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, prev, pager, next, sizes, jumper"
        :total="total"
      />
    </div>
  </div>
</template>

<script>
import { getLogList } from "@/api/sys";
import { parseTime } from "@/utils/index";
export default {
  name: "Log",
  data() {
    return {
      pickerOptions: this.$store.state.pickerOptions,
      form: {
        createBy: "",
        title: "",
        createTime: "",
        page: 1,
        limit: 10
      },
      createTime: "",
      dataList: [],
      total: 0,
      loading: false
    };
  },
  created() {
    this.getLogList();
  },
  methods: {
    getLogList() {
      this.loading = true;
      getLogList(this.form)
        .then(res => {
          let { records, total } = res.data;
          this.dataList = records;
          this.total = total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    search() {
      this.getLogList();
    },
    handleSizeChange(val) {
      this.form.limit = val;
      this.getLogList();
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getLogList();
    },
    changeDate(dateTime) {
      const startDate = parseTime(dateTime[0]);
      const endDate = parseTime(
        dateTime[1].getTime() + 24 * 60 * 60 * 1000 - 1
      );
      this.form.createTime = `${startDate},${endDate}`;
      // this.getLogList();
    },
    getCreateTime(time) {
      let createTime = new Date(time).getTime();
      return parseTime(createTime);
    },
    resetForm() {
      this.form = {
        createBy: "",
        title: "",
        createTime: "",
        page: 1,
        limit: 10
      };
      this.createTime = "";
      this.getLogList();
    },
    headerStyle() {
      return {
        background: "#F5F9FF",
        color: "#2E2E30"
      };
    },
    cellStyle() {
      return {
        height: "48px",
        padding: "0"
      };
    },
    getIndex($index) {
      return $index + (this.form.page - 1) * this.form.limit + 1;
    }
  }
};
</script>

<style lang="scss">
@import "~@/styles/page/list-page.scss";
.block-wrap {
  padding: 25px;
  background: #fff;
  border-radius: 2px;
  margin-bottom: 16px;
}
.el-form-item--small .el-form-item {
  margin-right: 60px;
}
::v-deep.list-page .filter-form .filter-form-btns {
  margin-right: 10px;
}
.dateTime {
  width: 350px;
  .el-form-item__content {
    width: 280px;
    .el-input__inner {
      width: 280px;
    }
  }
}
.list-page .filter-form .dateTime .el-form-item__content .el-input__inner {
  width: 280px;
}
</style>
